package web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.*;

/**
 * 进行敏感词汇的过滤
 * 参考文件：敏感词汇.txt
 *
 * @author Mc.Chai
 * @program: JavaWeb
 * @create 2022-04-08-18:07 18:07
 */
@WebFilter("/*")
public class SensitiveWordsFilter implements Filter {
    //敏感词汇的集合
    private  List<String> list = new ArrayList<>();
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        ServletRequest request_Proxy = (ServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), req.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //增强getParameter的返回值，
                if (method.getName().equals("getParameter")){
                    String invoke = (String) method.invoke(req, args);
                    //验证输入的值中是否含有敏感词汇
                    if (invoke!=null){
                        for (String s : list) {
                            if (invoke.contains(s)){
                                //含有敏感词汇
                                invoke = invoke.replaceAll(s, "***");
                            }
                        }
                        System.out.println(invoke+".....");
                        return invoke;
                    }
                }
                //getParameterMap()进行添加功能,需要创建一个新的map集合，获取的invoke集合是不能进行更改的
                if (method.getName().equals("getParameterMap")){
                    Map<String, String[]> invoke = (Map<String, String[]>) method.invoke(req, args);
                    HashMap<String, String[]> map = new HashMap<>();
                    if (invoke!=null){
                        Set<String> strings = invoke.keySet();
                        for (String string : strings) {
                            String value = invoke.get(string)[0];
                            //将所有的数据存放到map集合中
                            map.put(string,new String[]{value});
                            for (String s : list) {
                                //查看是否有敏感字符
                                if (value.contains(s)){
                                    //有，进行更改map集合中的数据
                                    value=value.replaceAll(s,"***");
                                    map.put(string,new String[]{value});
                                }
                            }

                        }



                    }
                    return map;
                }
                return method.invoke(req,args);
            }
        });
        //相同

        System.out.println("就是我"+request_Proxy);
        System.out.println(req);
        chain.doFilter(request_Proxy, resp);
    }
    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("------start-----/");
        ServletContext servletContext = config.getServletContext();
        String realPath = servletContext.getRealPath("/WEB-INF/classes/敏感词汇.txt");
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(realPath));
            while (true){
                String s = bufferedReader.readLine();
                if (s==null){break;}
                list.add(s);
            }
            System.out.println(list);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
